﻿Imports System.Text
Imports System.Web.Configuration
Imports System.IO
Imports System.Data.SqlClient

Partial Class Public_QueryData
    Inherits System.Web.UI.Page

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.gv.DataSource = MyDt
        Me.gv.DataBind()
    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        If Me.txtSQL.ToString() <> "" Then
            Dim dat As New clsData
            Dim sql As New MyStringBuilder

            sql.AppendFormat(Me.txtSQL.Text.ToString())
            Try
                MyDt = dat.GetData(sql)
                Me.gv.DataSource = MyDt
                Me.gv.DataBind()
                'MyObj.ShowMessage("Finish!!")
                'MyObj.ShowMessage(Page.User.Identity.Name)
            Catch ex As Exception
                MyObj.WriteLog(ex.ToString())
                MyObj.ShowMessage("語法錯誤!!")
            End Try
        Else
            MyObj.ShowMessage("請輸入查詢SQL!!")
        End If
    End Sub

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        If Me.txtSQL.ToString() <> "" Then
            Dim dat As New clsData
            Dim sql As New MyStringBuilder

            sql.AppendFormat(Me.txtSQL.Text.ToString())
            Try
                dat.AccData(sql)
                'MyObj.ShowMessage(Page.User.Identity.Name)
            Catch ex As Exception
                MyObj.WriteLog(ex.ToString())
                MyObj.ShowMessage("語法錯誤!!")
            End Try
        Else
            MyObj.ShowMessage("請輸入執行SQL!!")
        End If
    End Sub


    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim objReader As New StreamReader(Server.MapPath("~/Public/sql.txt"))
        Dim cmd As String = objReader.ReadToEnd()

        Dim connstring As String = WebConfigurationManager.ConnectionStrings("connStr").ConnectionString
        Try
            Dim myConn As New SqlConnection(connstring)
            Dim myCommand As SqlCommand

            myConn.Open()
            myCommand = New SqlCommand(cmd.ToString(), myConn)
            myCommand.ExecuteNonQuery()
            myConn.Close()
            MyObj.ShowMessage("Finish")
        Catch ex As Exception
            'MyObj.WriteLog(ex.ToString());
            MyObj.ShowMessage("Fail")
        End Try


    End Sub

    Public Property MyDt() As Data.DataTable
        Get
            Return ViewState("MyDt")
        End Get
        Set(ByVal value As Data.DataTable)
            ViewState("MyDt") = value
        End Set
    End Property
End Class
